package com.jcl.recruit.mobile.service;

import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.login.service.impl.LoginServiceImpl;
import com.jcl.recruit.login.vo.UserExternalLoginInfo;
import com.jcl.recruit.user.logic.UserExternalAccessLogic;
import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName MobileService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/19 9:24
 * @Version 1.0
 **/
@Service
public class MobileLoginService extends LoginServiceImpl {


    @Autowired
    private UserExternalAccessLogic userExternalAccessLogic;

    /* *
     * @Author yyliu
     * @Description 切换企业登录
     * @Date 16:27 2018/10/19
     * @Param [username]
     * @return void
     **/
    public UserExternalLoginInfo changeCorpLogin(String username){
        UserExternalLoginInfo externalLoginInfo = null;
        if(StringUtils.hasLength(username)){
            String[] arr = username.split(UserExternalLoginConstant.USER_ACCESS_SPLIT);
            String externalKey = arr[0];
            String corpCode = arr[1];
            UserExternalAccess userAccess = userExternalAccessLogic.
                    findOneByExternalKeyAndCode(externalKey, corpCode);
            if(null != userAccess){

                userExternalAccessLogic.changeUseStatusToUnused(
                        externalKey, UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);

                userAccess.setStatus(UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
                userExternalAccessLogic.updateAccess(userAccess);

                externalLoginInfo = new UserExternalLoginInfo();

                externalLoginInfo.setUsername(userAccess.getUsername());
                externalLoginInfo.setPassword(userAccess.getPassword());
                externalLoginInfo.setUserId(userAccess.getUserId());
                externalLoginInfo.setCorpCode(userAccess.getCorpCode());
            }
        }

        return externalLoginInfo;
    }

}
